package electionView;

import javax.swing.*;

import electionCtrl.Admin;
import electionCtrl.Election;

import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.Semaphore;

public final class AdminGui extends ElectionGui implements Comparable<AdminGui>
{
	/**
	 * This instance field is used in order to hole the arrayo of JButtons.
	 **/
	private final JButton[] adButtons;
	/**
	 * This instance field is used in order to hold the admin that is passed inside the constructor.
	 **/
	private final Admin admin;
	
	public enum Button
	{
		SET_USERNAME, SET_PASSWORD, /*GET_USERNAME, GET_PASSWORD, GET_VOTER_PASSWORDS,*/ MAKE_NEW_ADMIN, RETURN_TO_MAIN_MENU;
		
		public String toString()
		{
			String str = super.toString();
			return str.substring(0,1) + str.substring(1).toLowerCase().replaceAll("_", " ");
		}
	};
	
	private Button action;

	/**
	 * This is the construstor that instanciates all the buttons and instanciates the admin and election objects.
	 *@param admin This is the Admin object it takes in.
	 *@param election This is the Election object it takes in.
	 **/
	public AdminGui(Election election, Admin admin, Semaphore semaphore)
	{
		super(election, new JFrame("Admin View"), semaphore);
		this.action = null;
		this.admin = admin;
		this.adButtons = new JButton[Button.values().length];

		for(Button bt : Button.values())
		{
			ActionListener l = null;
			switch(bt)
			{
				case SET_USERNAME:
					l = new SetUsernameButtonListener();
					break;
				case SET_PASSWORD:
					l = new SetPasswordButtonListener();
					break;
				case MAKE_NEW_ADMIN:
					l = new MakeNewAdminButtonListener();
					break;
				case RETURN_TO_MAIN_MENU:
					l = new DoneButtonListener();
					break;
				default:
					throw new IllegalStateException();	
			}
			this.adButtons[bt.ordinal()] = new JButton(bt.toString());
			this.adButtons[bt.ordinal()].addActionListener(l);
		}
		this.mainFrame.setLayout(new FlowLayout());
		this.mainFrame.setSize(350,200);
		for(JButton button: this.adButtons)
		{
			this.mainFrame.add(button);
		}
	}
	
	public Admin getAdmin()
	{
		return this.admin;
	}
	
	public boolean checkUsername(String username)
	{
		return this.admin.checkUsername(username);
	}
	
	public boolean checkUsernamePrompt()
	{
		String username = JOptionPane.showInputDialog("Please enter your username:");
		return this.checkUsername(username);
	}
	
	public boolean checkPassword(String password)
	{
		return this.admin.checkPassword(password);
	}
	
	public boolean checkPasswordPrompt()
	{
		String password = JOptionPane.showInputDialog("Please enter your password:");
		return this.checkPassword(password);
	}
	
	public void setUsername(String username)
	{
		this.admin.setUsername(username);
	}
	
	public void setPassword(String password)
	{
		this.admin.setPassword(password);
	}
	
	private class SetUsernameButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.SET_USERNAME;
			releaseResources();
		}
	}
	
	private class SetPasswordButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.SET_PASSWORD;
			releaseResources();
		}
	}
	
	private class MakeNewAdminButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.MAKE_NEW_ADMIN;
			releaseResources();
		}
	}
	
	private class DoneButtonListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			action = Button.RETURN_TO_MAIN_MENU;
			releaseResources();
		}
	}
	
	public Button waitForAction() throws InterruptedException
	{
		this.acquireResources();
		Button b = this.action;
		this.action = null;
		return b;
	}
	
	public boolean equals(Object o)
	{
		if(!(o instanceof AdminGui))
		{
			return false;
		}
		AdminGui ag = (AdminGui)o;
		return ag.admin.equals(this.admin) && ag.election.equals(this.election);
	}

	@Override
	public int compareTo(AdminGui o)
	{
		return this.admin.compareTo(o.admin);
	}
}
